package com.example.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.crm.entity.SalesProcess;
import com.example.crm.entity.SalesStage;
import com.example.crm.entity.User;
import com.example.crm.mapper.SalesProcessMapper;
import com.example.crm.mapper.UserMapper;
import com.example.crm.service.SalesProcessService;
import com.example.crm.service.SalesStageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 销售流程服务实现类
 */
@Service
public class SalesProcessServiceImpl extends ServiceImpl<SalesProcessMapper, SalesProcess> implements SalesProcessService {

    @Autowired
    private SalesStageService salesStageService;
    
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<SalesProcess> listProcesses() {
        LambdaQueryWrapper<SalesProcess> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(SalesProcess::getIsDefault)
                    .orderByAsc(SalesProcess::getCreateTime);
        
        List<SalesProcess> processList = list(queryWrapper);
        
        // 填充创建人信息
        for (SalesProcess process : processList) {
            if (process.getCreatorId() != null) {
                User creator = userMapper.selectById(process.getCreatorId());
                process.setCreator(creator);
            }
        }
        
        return processList;
    }

    @Override
    public SalesProcess getProcessDetail(Long id) {
        SalesProcess process = getById(id);
        if (process != null) {
            // 查询流程阶段
            List<SalesStage> stages = salesStageService.listStagesByProcessId(id);
            process.setStages(stages);
            
            // 查询创建人
            if (process.getCreatorId() != null) {
                User creator = userMapper.selectById(process.getCreatorId());
                process.setCreator(creator);
            }
        }
        return process;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addProcess(SalesProcess process, List<SalesStage> stages) {
        // 保存流程
        boolean saved = save(process);
        if (saved && stages != null && !stages.isEmpty()) {
            // 批量保存阶段
            salesStageService.batchAddStages(stages, process.getId());
        }
        return saved;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateProcess(SalesProcess process, List<SalesStage> stages) {
        // 更新流程
        boolean updated = updateById(process);
        
        if (updated && stages != null) {
            // 删除原有阶段
            salesStageService.deleteStagesByProcessId(process.getId());
            
            // 批量添加新阶段
            if (!stages.isEmpty()) {
                salesStageService.batchAddStages(stages, process.getId());
            }
        }
        
        return updated;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setDefaultProcess(Long id) {
        // 先将所有流程设置为非默认
        LambdaUpdateWrapper<SalesProcess> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(SalesProcess::getIsDefault, false);
        update(updateWrapper);
        
        // 设置指定流程为默认
        SalesProcess process = new SalesProcess();
        process.setId(id);
        process.setIsDefault(true);
        
        return updateById(process);
    }

    @Override
    public SalesProcess getDefaultProcess() {
        LambdaQueryWrapper<SalesProcess> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SalesProcess::getIsDefault, true);
        
        SalesProcess process = getOne(queryWrapper);
        
        if (process != null) {
            // 查询流程阶段
            List<SalesStage> stages = salesStageService.listStagesByProcessId(process.getId());
            process.setStages(stages);
        }
        
        return process;
    }
} 